<?php
class ModelProductOption extends Model {
	public function addOption($data) {
		$name = $this->db->escape($data['name']);
		$this->db->query("INSERT INTO `" . DB_PREFIX . "option` SET `type` = '" . $this->db->escape($data['type']) . 
			"', `name` = '" . $name .
			"', option_unit = '" . $this->db->escape($data['option_unit']) . 
			"', min_size = '" . (int)$data['min_size'] . 
			"', max_size = '" . (int)$data['max_size'] . "'");
		$option_id = $this->db->getLastId();
		
		foreach ($data['option_description'] as $language_id => $value) {
			if($value['display_name']){
				$this->db->query("INSERT INTO " . DB_PREFIX . "option_description SET option_id = '" . (int)$option_id . 
					"', language_id = '" . (int)$language_id . "', display_name = '" . $this->db->escape($value['display_name']) . "'");
			}else{
				$this->db->query("INSERT INTO " . DB_PREFIX . "option_description SET option_id = '" . (int)$option_id . 
					"', language_id = '" . (int)$language_id . "', display_name = '" . $name . "'");
			}
		}

		if (isset($data['option_value'])) {
			foreach ($data['option_value'] as $option_value) {
				$name_value = $this->db->escape($option_value['name']);
				if($option_value['image']){
					$this->db->query("INSERT INTO " . DB_PREFIX . "option_value SET option_id = '" . (int)$option_id . 
						"', `name` = '" . $name_value . 
						"', image = '" . $this->db->escape(html_entity_decode($option_value['image'], ENT_QUOTES, 'UTF-8')) . 
						"', sort_order = '" . (int)$option_value['sort_order'] . "'");
				}else{
					$this->db->query("INSERT INTO " . DB_PREFIX . "option_value SET option_id = '" . (int)$option_id . 
						"', `name` = '" . $name_value . 
						"', sort_order = '" . (int)$option_value['sort_order'] . "'");
				}
				$option_value_id = $this->db->getLastId();
				
				foreach ($option_value['option_value_description'] as $language_id => $option_value_description) {
					if($option_value_description['display_name']){
						$this->db->query("INSERT INTO " . DB_PREFIX . "option_value_description SET option_value_id = '" . (int)$option_value_id . 
							"', language_id = '" . (int)$language_id . 
							"', display_name = '" . $this->db->escape($option_value_description['display_name']) . "'");
					}else{
						$this->db->query("INSERT INTO " . DB_PREFIX . "option_value_description SET option_value_id = '" . (int)$option_value_id . 
							"', language_id = '" . (int)$language_id . 
							"', display_name = '" . $name_value . "'");
					}
				}
			}
		}
	}
	
	public function editOption($option_id, $data) {
		$name = $this->db->escape($data['name']);
		$this->db->query("UPDATE `" . DB_PREFIX . "option` SET `type` = '" . $this->db->escape($data['type']) . 
			"', `name` = '" . $name .
			"', option_unit = '" . $this->db->escape($data['option_unit']) . 
			"', min_size = '" . (int)$data['min_size'] . 
			"', max_size = '" . (int)$data['max_size'] . 
			"' WHERE option_id = '" . (int)$option_id . "'");

		$this->db->query("DELETE FROM " . DB_PREFIX . "option_description WHERE option_id = '" . (int)$option_id . "'");
		foreach ($data['option_description'] as $language_id => $value) {
			if($value['display_name']){
				$this->db->query("INSERT INTO " . DB_PREFIX . "option_description SET option_id = '" . (int)$option_id . 
					"', language_id = '" . (int)$language_id . "', display_name = '" . $this->db->escape($value['display_name']) . "'");
			}else{
				$this->db->query("INSERT INTO " . DB_PREFIX . "option_description SET option_id = '" . (int)$option_id . 
					"', language_id = '" . (int)$language_id . "', display_name = '" . $name . "'");
			}
		}
		
		if (isset($data['option_value'])) {
			foreach ($data['option_value'] as $option_value) {
				$name_value = $this->db->escape($option_value['name']);
				if ($option_value['option_value_id']) {
					$option_value_id = $option_value['option_value_id'];
					$sql_value = "UPDATE " . DB_PREFIX . "option_value SET option_id = '" . (int)$option_id . 
						"', `name` = '" . $name_value;
					if($option_value['image']){
						$sql_value .= "', image = '" . $this->db->escape(html_entity_decode($option_value['image'], ENT_QUOTES, 'UTF-8')) . "'";
					}else{
						$sql_value .= "', image = null";
					}
					$sql_value .= ", sort_order = '" . (int)$option_value['sort_order'] . 
						"' WHERE option_value_id = '" . (int)$option_value_id . "'";
					$this->db->query($sql_value);
				} else {
					$sql_value = "INSERT INTO " . DB_PREFIX . "option_value SET option_id = '" . (int)$option_id . 
						"', `name` = '" . $name_value;
					if($option_value['image']){
						$sql_value .= "', image = '" . $this->db->escape(html_entity_decode($option_value['image'], ENT_QUOTES, 'UTF-8'));
					}
					$sql_value .= "', sort_order = '" . (int)$option_value['sort_order'] . "'";
					$this->db->query($sql_value);
					$option_value_id = $this->db->getLastId();
				}
				
				$this->db->query("DELETE FROM " . DB_PREFIX . "option_value_description WHERE option_value_id = '" . (int)$option_value_id . "'");
				foreach ($option_value['option_value_description'] as $language_id => $option_value_description) {
					if($option_value_description['display_name']){
						$this->db->query("INSERT INTO " . DB_PREFIX . "option_value_description SET option_value_id = '" . (int)$option_value_id . 
							"', language_id = '" . (int)$language_id . 
							"', display_name = '" . $this->db->escape($option_value_description['display_name']) . "'");
					}else{
						$this->db->query("INSERT INTO " . DB_PREFIX . "option_value_description SET option_value_id = '" . (int)$option_value_id . 
							"', language_id = '" . (int)$language_id . 
							"', display_name = '" . $name_value . "'");
					}
				}
			}
		}
	}
	
	public function getOption($option_id) {
		$query = $this->db->query("SELECT o.*, od.display_name FROM `" . DB_PREFIX . "option` o LEFT JOIN " . 
			DB_PREFIX . "option_description od ON (o.option_id = od.option_id AND od.language_id = '" . 
			(int)$this->config->get('config_language_id') . "') WHERE o.option_id = '" . (int)$option_id . "'");
		return $query->row;
	}
	
	public function getOptionDescriptions($option_id) {
		$option_data = array();
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "option_description WHERE option_id = '" . (int)$option_id . "'");
		foreach ($query->rows as $result) {
			$option_data[$result['language_id']] = array(
				'option_id' => $result['option_id'],
				'display_name' => $result['display_name'],
			);
		}
		return $option_data;
	}
	
	public function getOptionValues($option_id) {
		$option_value_data = array();
		$option_value_query = $this->db->query("SELECT ov.*, ovd.display_name FROM " . DB_PREFIX . "option_value ov LEFT JOIN " . 
			DB_PREFIX . "option_value_description ovd ON (ov.option_value_id = ovd.option_value_id AND ovd.language_id = '" . 
			(int)$this->config->get('config_language_id') . "') WHERE ov.option_id = '" . (int)$option_id . "' ORDER BY ov.sort_order ASC");
		foreach ($option_value_query->rows as $option_value) {
			$option_value_data[] = array(
				'option_value_id' => $option_value['option_value_id'],
				'name'            => $option_value['name'],
				'display_name'    => $option_value['display_name'],
				'image'           => $option_value['image'],
				'sort_order'      => $option_value['sort_order']
			);
		}
		return $option_value_data;
	}
	
	public function getOptionValueDescriptions($option_id) {
		$option_value_data = array();
		$option_value_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "option_value WHERE option_id = '" . (int)$option_id . "'");
		foreach ($option_value_query->rows as $option_value) {
			$option_value_description_data = array();
			$option_value_description_query = $this->db->query("SELECT * FROM " . 
				DB_PREFIX . "option_value_description WHERE option_value_id = '" . (int)$option_value['option_value_id'] . "'");			
			foreach ($option_value_description_query->rows as $option_value_description) {
				$option_value_description_data[$option_value_description['language_id']] = array(
					'option_value_id' => $option_value_description['option_value_id'],
					'display_name' => $option_value_description['display_name']
				);
			}
			$option_value_data[] = array(
				'option_value_id'          => $option_value['option_value_id'],
				'name'                     => $option_value['name'],
				'image'                    => $option_value['image'],
				'sort_order'               => $option_value['sort_order'],
				'option_value_description' => $option_value_description_data
			);
		}
		
		return $option_value_data;
	}

	public function getOptions($data = array()) {
		$sql = "SELECT o.*, od.display_name FROM `" . DB_PREFIX . "option` o LEFT JOIN " . 
			DB_PREFIX . "option_description od ON (o.option_id = od.option_id AND od.language_id = '" . 
			(int)$this->config->get('config_language_id') . "')";
		
		$sql_filter = "";
		if (isset($data['filter_display_name']) && $data['filter_display_name'] != "") {
			$sql_filter .= " AND LCASE(od.display_name) LIKE '" . $this->db->escape($data['filter_display_name']) . "%'";
		}
		if (isset($data['filter_type']) && $data['filter_type'] != "-1") {
			$sql_filter .= " AND o.`type` = '" . $this->db->escape($data['filter_type']) . "'";
		}
		if($sql_filter != ""){
			$sql_filter = trim($sql_filter);
			$sql .= " WHERE " . ltrim($sql_filter, "AND");
		}
		
		$sort_data = array(
			'od.display_name',
			'o.type'
		);	
		if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
			$sql .= " ORDER BY " . $data['sort'];	
		} else {
			$sql .= " ORDER BY od.display_name";	
		}
		if (isset($data['order']) && ($data['order'] == 'DESC')) {
			$sql .= " DESC";
		} else {
			$sql .= " ASC";
		}
		
		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}					
			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}	
			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}	
		
		$query = $this->db->query($sql);
		return $query->rows;
	}

	public function getTotalOptions($data = array()) {
		$sql = "SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "option` o LEFT JOIN " . 
			DB_PREFIX . "option_description od ON (o.option_id = od.option_id AND od.language_id = '" . 
			(int)$this->config->get('config_language_id') . "')";
		
		$sql_filter = "";
		if (isset($data['filter_display_name']) && $data['filter_display_name'] != "") {
			$sql_filter .= " AND LCASE(od.display_name) LIKE '" . $this->db->escape($data['filter_display_name']) . "%'";
		}
		if (isset($data['filter_type']) && $data['filter_type'] != "-1") {
			$sql_filter .= " AND o.`type` = '" . $this->db->escape($data['filter_type']) . "'";
		}
		if($sql_filter != ""){
			$sql_filter = trim($sql_filter);
			$sql .= " WHERE " . ltrim($sql_filter, "AND");
		}
      	$query = $this->db->query($sql); 
		return $query->row['total'];
	}		
}
?>